
package de.justphil.tcg.tcgandroidclient.gui.decks;

import java.util.ArrayList;
import java.util.List;

import de.justphil.tcg.tcgandroidclient.Storage;
import de.justphil.tcg.tcgandroidclient.content.TCGContentAdapter;
import de.justphil.tcg.tcgandroidclient.content.TCGUM;
import de.justphil.tcg.tcgandroidclient.content.sqlite.SqliteDeck;
import de.justphil.tcg.tcgandroidclient.content.view.ViewDeck;
import de.justphil.tcg.tcgandroidclient.gui.DeckCreatorAct;
import de.justphil.tcg.tcgandroidclient.network.rest.RESTUpdateService;
import android.app.ListActivity;
import android.content.Intent;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class DecksAct extends ListActivity {
	
	private TCGContentAdapter contentAdapter;
	private ContentObserver coUseGetter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		contentAdapter = new TCGContentAdapter(this);
		final ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			
				ViewDeck deck = (ViewDeck) lv.getItemAtPosition(position);
				Intent intent = new Intent(DecksAct.this, DeckCreatorAct.class);
				intent.setData(TCGUM.getDeckUri(Storage.getPlayerId(), deck.getDeckId()));
				startActivity(intent);
			}
		});
	}
	
	@Override
	protected void onStart() {
	
		super.onStart();
	}
	
	@Override
	protected void onPause() {
	
		super.onPause();
		contentAdapter.unregisterContentObserver(getContentObserver());
	}
	
	@Override
	protected void onResume() {
	
		super.onResume();
		Intent intent = new Intent(DecksAct.this, RESTUpdateService.class);
		intent.putExtra(RESTUpdateService.EXTRA_CLEAR_CACHE, true);
		startService(intent);
		setListAdapter(createArrayAdapter());
	}
	
	private ContentObserver getContentObserver() {
	
		if (coUseGetter == null) {
			coUseGetter = new ContentObserver(new Handler()) {
				
				@Override
				public void onChange(boolean selfChange) {
				
					setListAdapter(createArrayAdapter());
				}
			};
		}
		return coUseGetter;
	}
	
	private ArrayAdapter<ViewDeck> createArrayAdapter() {
	
		List<SqliteDeck> sqliteDecks = contentAdapter.readDecks(getContentObserver());
		List<ViewDeck> decks = new ArrayList<ViewDeck>();
		for (SqliteDeck sqliteDeck : sqliteDecks) {
			ViewDeck deck =
					new ViewDeck(contentAdapter, getContentObserver(), sqliteDeck.getId(),
							sqliteDeck.getName());
			decks.add(deck);
		}
		ArrayAdapter<ViewDeck> adapter =
				new ArrayAdapter<ViewDeck>(this, android.R.layout.simple_list_item_1,
						android.R.id.text1, decks);
		return adapter;
	}
}
